package com.dmzc.stage1.chapter1;

import java.util.concurrent.TimeUnit;

/***
 * 修改为多线程
 */
public class TryConcurrency1 {
    public static void main(String[] args) {
        new Thread("browsenews"){
            @Override
            public void run() {
                browsenews();
            }
        }.start();
        enjoyMusic();
    }

    private static void enjoyMusic() {
        for (;;){
            System.out.println("nice music");
            sleep(1);
        }
    }

    private static void sleep(int seconds) {
        try {
            //提供对Thread.sleep的封装，只是添加了时间转换
            //都是在Main线程执行，所以无法交替运行
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void browsenews() {
        for (;;){
            System.out.println("good news");
            sleep(1);
        }
    }
}
